package com.kakame.dibaaz.annotation.handler;

import java.lang.reflect.Method;
import java.util.Properties;

import com.kakame.dibaaz.annotation.ToView;
import com.kakame.dibaaz.box.WebBox;
import com.kakame.dibaaz.sieve.DibaazSieve;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class ToViewHandler implements MethodInterceptor {

	private static final Properties SYSTEM_CONFIG_PRO = DibaazSieve.SYSTEM_CONFIG_PRO;
	
	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		WebBox webBox = null;
		webBox = (WebBox) proxy.invokeSuper(obj, args);
		if (method.isAnnotationPresent(ToView.class)) { // 检查该方法上是否有ToView注解
			ToView toView = method.getAnnotation(ToView.class); // 取得注解
			String page = SYSTEM_CONFIG_PRO.getProperty("dibaaz.url.prefix") + toView.view() + SYSTEM_CONFIG_PRO.getProperty("dibaaz.url.suffix");
			webBox.setResponseUrl(page);
		}
		return webBox;
	}

}